﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using StairsModuleAndEdit.StairsEdit.Common;

namespace StairsModuleAndEdit.StairsEdit.Managers
{
	internal class JudgeHidingCls
	{
		public JudgeHidingCls(ExternalCommandData commandData)
		{
			this.m_UiDoc = commandData.Application.ActiveUIDocument;
			this.m_doc = this.m_UiDoc.Document;
		}

		public void FindHidingStairs(out List<ElementId> HideStairs, out List<ElementId> EditStairs)
		{
			HideStairs = new List<ElementId>();
			EditStairs = new List<ElementId>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			IList<Element> list = filteredElementCollector.OfClass(typeof(Stairs)).WhereElementIsNotElementType().ToElements();
			foreach (Element element in list)
			{
				if (element.IsHidden(this.m_doc.ActiveView))
				{
					if (element.CanBeHidden(this.m_doc.ActiveView))
					{
						HideStairs.Add(element.Id);
					}
				}
				else
				{
					ElementId elementId = null;
					bool flag = StairsXDataManager.ReadArchStairsId(element, ref elementId);
					if (flag && elementId == ElementId.InvalidElementId)
					{
						EditStairs.Add(element.Id);
					}
				}
			}
		}

		public void FindneedHideStairs(out List<ElementId> ArchStairs, out List<ElementId> EditStairs)
		{
			ArchStairs = new List<ElementId>();
			EditStairs = new List<ElementId>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			List<Element> list = filteredElementCollector.OfClass(typeof(Stairs)).WhereElementIsNotElementType().ToElements() as List<Element>;
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i] != null)
				{
					Stairs stairs = list[i] as Stairs;
					ElementId ArchId = null;
					bool flag = StairsXDataManager.ReadArchStairsId(stairs, ref ArchId);
					if (flag)
					{
						if (ArchId != null)
						{
							int index = list.FindIndex((Element s) => s != null && s.Id == ArchId);
							ArchStairs.Add(list[index].Id);
							list[index] = null;
						}
					}
					else
					{
						StairsType stairsType = this.m_doc.GetElement(stairs.GetTypeId()) as StairsType;
						StairsRunType stairsRunType = this.m_doc.GetElement(stairsType.RunType) as StairsRunType;
						if (stairsRunType.TreadThickness > CommonUnit._epsDouble && stairsRunType.HasRisers && stairsRunType.StructuralDepth > CommonUnit._epsDouble)
						{
							EditStairs.Add(stairs.Id);
							list[i] = null;
						}
					}
				}
			}
			for (int j = 0; j < list.Count; j++)
			{
				if (list[j] != null)
				{
					Stairs stairs2 = list[j] as Stairs;
					StairsType stairsType2 = this.m_doc.GetElement(stairs2.GetTypeId()) as StairsType;
					StairsRunType stairsRunType2 = this.m_doc.GetElement(stairsType2.RunType) as StairsRunType;
					if (stairsRunType2.TreadThickness > CommonUnit._epsDouble && stairsRunType2.HasRisers)
					{
						EditStairs.Add(stairs2.Id);
						list[j] = null;
					}
				}
			}
		}

		private Document m_doc;

		private UIDocument m_UiDoc;
	}
}
